package com.example.xunua_mymusic;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Toast;

public class welcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        getPermission();

    }
    void thread(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                SystemClock.sleep(3000);
                SharedPreferences sharedPreferences=getSharedPreferences("welcome",MODE_PRIVATE);
                SharedPreferences.Editor editor=sharedPreferences.edit();
                boolean flag = sharedPreferences.getBoolean("flag", false);
                if (flag) {
                    Intent intent=new Intent(welcomeActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }else {
                    editor.putBoolean("flag",true);
                    editor.commit();
                    Intent intent=new Intent(welcomeActivity.this,guideActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        }).start();
    }
    public void getPermission(){
        String[] permissions=new String[]{Manifest.permission.READ_EXTERNAL_STORAGE};
        boolean isAllGranted=checkPermissionAllGranted(permissions);// 检测是否有该权限
        if (isAllGranted) {
            //有的话执行这句
            thread();
        }else {
            //没有的话则申请获取权限
            ActivityCompat.requestPermissions(welcomeActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},999);
        }
    }
    //检测是否有该权限
    public boolean checkPermissionAllGranted(String[] permissions){
        for (String permission: permissions)
        {
            if (ContextCompat.checkSelfPermission(getApplicationContext(),permission)!= PackageManager.PERMISSION_GRANTED){
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==999){
            thread();
        }
    }
}
